// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

// import Vue from 'vue'
import App from './App'
import store from './store'
import router from './router'

router.beforeEach((to, from, next) => {
  // 拦截路由跳转到指定页面
  // if (to.query.businessType == 1) {
  //   next({
  //     name: 'BillList2',
  //   })
  // } else {
  //   next();
  // }
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

Vue.config.productionTip = false

import axios from 'axios'

// localStorage.setItem('token', 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImNkNDE2ZWNmLTliYTUtNDFiNi05NjQyLWUwZTY0Nzk3ZTU3MSJ9.t6hCkpguWNk61yrLa24EygNGboTJq4TWPZr_U4iu1tND0a7Cxdplom3UBAbK4u9ES92ZLiXApkzxbn9tUCfnKA')
Vue.prototype.axios = axios;
axios.defaults.timeout = 30 * 1000; // 30s
axios.interceptors.request.use(config=>{
  let token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = 'Bearer ' + token;
  }
  return config;
},err=>{
  return Promise.reject(err);
});
axios.interceptors.response.use(
  response => {
    if (response.config.url && response.config.url.indexOf('.action') > -1 && response.config.url != '/system/Tuser!getWxUser.action') {
      if (response.data.msg == 'outlogin') {
        $.toast('会话已过期，请重新登录！', 1000);
        setTimeout(function () {
          // location.href = '/weixin/WechatLogin!loginpage.action';
          location.href = '/#/signin';
        }, 1000)
      } else if (response.data.msg == 'sessionTimeOut') {
        $.toast('登录超时，请重新登入系统！', 1000);
        setTimeout(function () {
          WeixinJSBridge.call("closeWindow");
        }, 1000)
      }
    }
    //如果每个接口都校验会导致
    // if (response.config.url) {
    //   if (response.data.code == 401) {
    //     localStorage.setItem('openid', data.msg);
    //     $.toast('用户已过期，请重新登录！', 1000);
    //     setTimeout(() => {
    //       location.href = '/#/signin';
    //     }, 1500)
    //   } else if (response.data.code == 502) {
    //     $.toast('会话过期！', 1000);
    //     if (typeof WeixinJSBridge == "undefined") {
    //       setTimeout(() => {
    //         location.href = '/#/signin';
    //       }, 1500)
    //     } else {
    //       setTimeout(function() {
    //         WeixinJSBridge.call("closeWindow");
    //       }, 1500)
    //     }
    //   }
    // }
    return response;
  }
);

import WeVue from 'we-vue'
// import 'we-vue/lib/style.css'
Vue.use(WeVue)

//全局变量
import base from './base'
Vue.use(base);

import uploader from 'vue-simple-uploader'
Vue.use(uploader)

import VModal from 'vue-js-modal'
Vue.use(VModal)

import VueWater from 'vue-water-mark-tools/src/lib/index';
Vue.use(VueWater);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
